package com.catstore.controller;

import com.catstore.entity.Book;
import com.catstore.service.BookService;
import com.catstore.utils.Constant;
import com.catstore.model.Message;
import com.catstore.utils.messageUtils.MessageUtil;
import com.catstore.utils.sessionUtils.SessionUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {

    BookService bookService;

    @Autowired
    void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping("/getBooks")
    List<Book> getBooks() {
        return bookService.getBooks();
    }

    @GetMapping("/getBooksByPage")
    Message getBooks(@RequestParam int page) {
        return bookService.getBooks(page);
    }

    @GetMapping("/getBookTitle")
    String getBookTitleByBookId(@RequestParam("bookId") Integer bookId) {
        return bookService.getBookTitleByBookId(bookId);
    }

    @GetMapping("/getBooksByKeyword")
    List<Book> getBooksByKeyword(@RequestParam("keyword") String keyword) {
        return bookService.getBooksByKeyword(keyword);
    }

    @GetMapping("/getBookById")
    Book getBookById(@RequestParam("bookId") Integer bookId) {
        return bookService.getBookById(bookId);
    }

    @GetMapping("/getConcernedBookInfo")
    List<Map<String, String>> getConcernedBookInfo(@RequestParam("bookTitle") String bookTitle, @RequestParam("website") String websiteSrc) {
        return bookService.getConcernedBookInfo(bookTitle, websiteSrc);
    }

    @PostMapping("/manager/deleteBooks")
    Message deleteBooks(@RequestBody ArrayList<Integer> bookIdList) {
        Integer userIdentity = SessionUtil.getUserIdentity();
        if (userIdentity != null && userIdentity.equals(Constant.MANAGER)) {
            if (bookService.deleteBooks(bookIdList))
                return MessageUtil.createMessage(MessageUtil.DELETE_SUCCESS_CODE, MessageUtil.DELETE_SUCCESS_MSG);
            else
                return MessageUtil.createMessage(MessageUtil.DELETE_FAIL_CODE, MessageUtil.DELETE_FAIL_MSG);
        }
        return MessageUtil.createMessage(MessageUtil.HAVE_NO_AUTHORITY_CODE, MessageUtil.HAVE_NO_AUTHORITY_MSG);
    }

    @PostMapping("/manager/undercarriage")
    Message undercarriage(@RequestBody ArrayList<Integer> bookIdList) {
        Integer userIdentity = SessionUtil.getUserIdentity();
        if (userIdentity != null && userIdentity.equals(Constant.MANAGER)) {
            System.out.println(bookIdList.toString());
            if (bookService.undercarriage(bookIdList))
                return MessageUtil.createMessage(MessageUtil.UNDERCARRIAGE_SUCCESS_CODE, MessageUtil.UNDERCARRIAGE_SUCCESS_MSG);
            else
                return MessageUtil.createMessage(MessageUtil.UNDERCARRIAGE_FAIL_CODE, MessageUtil.UNDERCARRIAGE_FAIL_MSG);
        }
        return MessageUtil.createMessage(MessageUtil.HAVE_NO_AUTHORITY_CODE, MessageUtil.HAVE_NO_AUTHORITY_MSG);
    }

    @PostMapping("/manager/putOnSale")
    Message putOnSale(@RequestBody ArrayList<Integer> bookIdList) {
        Integer userIdentity = SessionUtil.getUserIdentity();
        if (userIdentity != null && userIdentity.equals(Constant.MANAGER)) {
            System.out.println(bookIdList.toString());
            if (bookService.putOnSale(bookIdList))
                return MessageUtil.createMessage(MessageUtil.PUT_ON_SALE_SUCCESS_CODE, MessageUtil.PUT_ON_SALE_SUCCESS_MSG);
            else
                return MessageUtil.createMessage(MessageUtil.PUT_ON_SALE_FAIL_CODE, MessageUtil.PUT_ON_SALE_FAIL_MSG);
        }
        return MessageUtil.createMessage(MessageUtil.HAVE_NO_AUTHORITY_CODE, MessageUtil.HAVE_NO_AUTHORITY_MSG);
    }

    @PostMapping("/getRankedBooks")
    ArrayList<Book> getRankedBooks(@RequestBody ArrayList<Date> startNEndDates) {
        return bookService.getRankedBooks(startNEndDates);
    }

    @PostMapping("/manager/postModifiedBook")
    void postModifiedBook(@RequestBody Map<String, String> book) {
        System.out.println(book.toString());
        bookService.postModifiedBook(book);
    }
}
